package com.xauto.pioneer.appuser.activity.mine;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.ParkingOrderAdapter;
import com.xauto.pioneer.appuser.model.ParkingOrderInfo;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Administrator on 2018/5/19.
 */

public class ParkingOrderActivity extends BaseActivity {
    private PromptDialog promptDialog;
    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.list_parkingorder)
    PullToRefreshListView pullToRefreshListView;
    ListView list;
    ParkingOrderAdapter adapter;
    private List<ParkingOrderInfo> parkingorderInfoList;
    @Override
    protected int getContentView() {
        return R.layout.ac_parkingorder;
    }

    @Override
    protected void initView() {

//        EventBus.getDefault().register(this);
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setText(TopBar.TITLE, R.string.mine_parkingorder);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        list = pullToRefreshListView.getRefreshableView();
        list.setVerticalScrollBarEnabled(false);
        list.setDividerHeight(3);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("tag","id = "+id+"--"+position);
//                //当前点击订单
//                ParkingOrderInfo currentOrderInfo =  adapter.getItem(position-1);
//                OrderDetailActivity.startAction(mContext,currentOrderInfo.car_order_no);
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //刷新
                page = 1;
                refreshList();
//                load();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉
                if (isMore){
                    page ++;
                    refreshList();
//                    load();
                }else {
                    pullToRefreshListView.onRefreshComplete();
                    prompt("暂无更多数据");
                    refreshList();
                }
            }
        });
    }
    void refreshList() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (page == 1){adapter.getDataList().clear();}
//                Collections.reverse(orderInfoList);
                adapter.getDataList().addAll(parkingorderInfoList);
                adapter.notifyDataSetChanged();
            }
        });
    }
    @Override
    protected void initData() {
        adapter = new ParkingOrderAdapter(this, R.layout.item_parkingorder);
        list.setAdapter(adapter);
        page = 1;
        parkingorderInfoList = new ArrayList<>();
        JSONObject Json;
        JSONArray JsonArray = new JSONArray();
        for (int i = 0; i < 2; i++) {
            Json = new JSONObject();
            try {
                Json.put("parking_order_location","华清学府");
                Json.put("parking_order_price","15");
                Json.put("parking_order_carnum","陕A11111");
                Json.put("parking_order_time","2小时22分22秒");
                Json.put("parking_order_start","2018-04-04  12：22:22");
                Json.put("parking_order_end","2018-04-04  13：22:22");
                JsonArray.put(Json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        Log.e("tag",JsonArray.toString());
        for (int i = 0; i < JsonArray.length(); i++) {
         parkingorderInfoList.add(new ParkingOrderInfo().parse(JsonArray.optJSONObject(i)));
        }
        adapter.getDataList().addAll(parkingorderInfoList);
        adapter.notifyDataSetChanged();
    }
    private void showNoDataView(){
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_listview_empty, null);
        ((TextView) emptyView.findViewById(R.id.tvEmptyNotice)).setText("暂无订单");
        ((ViewGroup)list.getParent()).addView(emptyView);
        list.setEmptyView(emptyView);
    }
}
